//
//  ZHFPlaceCell.m
//  GoTraveling
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "ZHFPlaceCell.h"

#import "ZHFPlaceItem.h"

#import "ZHFPlace.h"

@interface ZHFPlaceCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic,strong) UICollectionView *collectionView;


@end

@implementation ZHFPlaceCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2, (SCREEN_WIDTH - 32 * ADAPT_WIDTH) / 2);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10 * ADAPT_WIDTH, 10, 10 * ADAPT_WIDTH);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 720) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.96 blue:0.91 alpha:1];
    self.collectionView.backgroundColor = self.contentView.backgroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[ZHFPlaceItem class] forCellWithReuseIdentifier:@"PlaceCell"];
    [self.contentView addSubview:self.collectionView];
}


#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
//    NSLog(@"dddd == %ld",_placeArray.count);
    return self.placeArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //协议代理第三步:使用代理人执行协议方法
    [self.delegate pushAttrationViewControllerWithId:[[self.placeArray objectAtIndex:indexPath.row] Id]];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ZHFPlaceItem *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PlaceCell" forIndexPath:indexPath];
    cell.backgroundColor = self.contentView.backgroundColor;
    cell.place = [self.placeArray objectAtIndex:indexPath.row];
    
//    NSLog(@"fff == %ld",self.placeArray.count);
    
    return cell;
}

#pragma mark - placeArray set方法
- (void)setPlaceArray:(NSMutableArray *)placeArray {
    
    _placeArray = placeArray;
    
    [self.collectionView reloadData];
    
//    NSLog(@"eee == %ld",placeArray.count);
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
